package com.erp.notifiactionDb;

import com.erp.util.Sputil;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/21/021.
 * 作者：刘洋
 */
public class PersonDB {
    public static String DB_NAME;
    //数据库名
    public static final int VERSION = 1;
    //数据库版本号
    private static PersonDB personDB;
    private static DbManager db;
    //接收构造方法初始化的DbManager对象
    //构造方法私有化,拿到DbManager对象
    private PersonDB(){
        DB_NAME=Sputil.sp("erp").getString("mail","")+".db";
        DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(DB_NAME,VERSION);
        db = x.getDb(databaseOpenHelper.getDaoConfig());
    }
    //获取PersonDB实例
    public synchronized static PersonDB getIntance(){
        if (personDB == null){
            personDB = new PersonDB();
        }
        return personDB;
    }

    public void clear(){
        personDB=null;
    }
    /****************************************************************************************/
    //写两个测试方法，也就是常用的数据库操作
    //将Person实例存进数据库
    public void savePerson(NotificationBean notificationBean){
        try {
            db.save(notificationBean);
        } catch (DbException e) {
        }
    }
    //读取所有Person信息
    public List<NotificationBean> loadPerson(){
        List<NotificationBean> list = null;
        try {
            list = db.selector(NotificationBean.class).orderBy("notification",true).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        if(list==null){
            list=new ArrayList<>();
        }
        return list;
    }
    //删除所有Person信息
    public void delete(){
        List<NotificationBean> list = null;
        try {
            db.delete(NotificationBean.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    //修改Person信息
    public void updata(NotificationBean notificationBean){
        try {
            db.update(notificationBean);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    //根据通知id查询
    public List<NotificationBean> findnotification(int notification){
        List<NotificationBean> notification1;
        try {
            notification1 = db.selector(NotificationBean.class).where("notification","=",notification).findAll();
            return notification1;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }
    //根据通知id查询
    public List<NotificationBean> findfase(boolean istrue){
        List<NotificationBean> notification1;
        try {
            notification1 = db.selector(NotificationBean.class).where("istrue","=",istrue).limit(1).findAll();
            return notification1;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }
    //降序排序
    public void jiangxu(String st){
        try {
             db.selector(NotificationBean.class).orderBy(st,false).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    /**
     *查询一定数量的数据
     * s : 查询字段
     * offset ：开始的序号
     * limit ：获取数据数量
     * */
    public List<NotificationBean> load(String s,int limit,int offset){
        List<NotificationBean> list = null;
        try {
            list = db.selector(NotificationBean.class).orderBy(s,true).limit(limit).offset(offset).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        if(list==null){
            list=new ArrayList<>();
        }
        return list;
    }
}
